<template>
  <el-dialog
    :title="!dataForm.courseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程名称" prop="courseName">
      <el-input v-model="dataForm.courseName" placeholder="课程名称"></el-input>
    </el-form-item>
    <el-form-item label="课程学分" prop="courseCredit">
      <el-input v-model="dataForm.courseCredit" placeholder="课程学分"></el-input>
    </el-form-item>
    <el-form-item label="课程类型" size="mini" prop="courseType">
        <el-radio-group v-model="dataForm.courseType">
          <el-radio :label="1">必修</el-radio>
          <el-radio :label="0">选修</el-radio>
        </el-radio-group>
      </el-form-item>
    <el-form-item label="课程时间" prop="time">
      <el-date-picker
        v-model="dataForm.time"
        type="datetimerange"
        value-format="yyyy-MM-dd HH:mm:ss"
        range-separator="至"
        start-placeholder="课程开始日期"
        end-placeholder="课程结束日期"
        align="left">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="容纳学生" prop="totalNum">
      <el-input style="width:150px" v-model="dataForm.totalNum" placeholder="课程容纳学生总数"></el-input>
    </el-form-item>
    <el-form-item label="授课老师" prop="userId">
        <el-select v-model="dataForm.userId" filterable placeholder="请选择">
          <el-option
            v-for="item in selectList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        selectList: [],
        dataForm: {
          courseId: 0,
          courseName: '',
          courseCredit: '',
          courseType: '',
          time: [],
          totalNum: '',
          userId: '',
        },
        dataRule: {
          courseName: [
            { required: true, message: '课程名称不能为空', trigger: 'blur' }
          ],
          courseCredit: [
            { required: true, message: '课程学分不能为空', trigger: 'blur' }
          ],
          courseType: [
            { required: true, message: '课程类型不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '课程时间不能为空', trigger: 'blur' }
          ],
          totalNum: [
            { required: true, message: '课程容纳学生总数不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '授课老师不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.courseId = id || 0
        this.visible = true
        this.$http({
        url: this.$http.adornUrl("/generator/course/getAllTeacher"),
        method: "get",
        params: this.$http.adornParams(),
        }).then(({ data }) => {
          this.selectList = data.user;
          this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.courseId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/course/info/${this.dataForm.courseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.courseName = data.course.courseName
                this.dataForm.courseCredit = data.course.courseCredit
                this.dataForm.courseType = data.course.courseType
                this.dataForm.totalNum = data.course.totalNum
                this.dataForm.time = [data.course.startTime, data.course.endTime]
                this.dataForm.userId = data.course.userId
              }
            })
          }
          })
        })
        
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/course/${!this.dataForm.courseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'courseId': this.dataForm.courseId || undefined,
                'courseName': this.dataForm.courseName,
                'courseCredit': this.dataForm.courseCredit,
                'courseType': this.dataForm.courseType,
                'startTime': this.dataForm.time[0],
                'endTime': this.dataForm.time[1],
                'totalNum': this.dataForm.totalNum,
                'userId': this.dataForm.userId,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
